package flex.cc.drive.api;

import java.net.URLDecoder;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import flex.cc.common.utils.DateUtil;
import flex.cc.core.support.BaseController;
import flex.cc.drive.service.CheckScopeService;
import flex.cc.drive.service.DriverCancelService;
import flex.cc.drive.service.OrderDriverService;

/**
 * 代驾类接口测试
 */
@RestController
@RequestMapping(value = "/storeapi")
public class DriveController extends BaseController {

	@Resource
    private OrderDriverService orderDriverService;
	@Resource
    private CheckScopeService checkScopeService;
	@Resource
	private DriverCancelService driverCancelService;
    /**
     * 预约新增接口
     */
	@ResponseBody
    @RequestMapping("/designatedDriver")
    public JSONObject designatedDriver(HttpServletRequest request) throws Exception {
		JSONObject jsonResult = new JSONObject();
        // 进入接口时间
        long startTime = System.currentTimeMillis();
        logger.info("storeapi/designatedDriver 进入接口:time=" + DateUtil.getStrDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));

        Boolean flag = true;
        String responseMsg = "成功";
        //读取报文
        try {
            // 设置编码
            request.setCharacterEncoding("UTF-8");
            // 读取请求报文
            String requestJson = IOUtils.toString(request.getInputStream(), "UTF-8");
            requestJson = URLDecoder.decode(requestJson, "UTF-8");
            if (requestJson != null && !"".equals(requestJson)) {
                // 调用业务处理 =============================
            	logger.info("storeapi/designatedDriver requestJson:" + requestJson);
                JSONObject requestBody = JSONObject.parseObject(requestJson);
                Map<String, Object> requestBodyMap = requestBody.toJavaObject(Map.class);
                //请求下游
                flag = true;
                jsonResult = orderDriverService.createOrUpdateBespeakOrder(requestBodyMap); 
                logger.info("storeapi/designatedDriver： "+jsonResult);
            } else {
                flag = false;
                responseMsg = "接口处理异常:请求报文为空";
                logger.error("storeapi/designatedDriver 接口处理异常:请求报文为空");
            }
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
            responseMsg = "数据接收异常";
            logger.error("storeapi/designatedDriver 数据接收异常:" + e.getMessage());
        }
        if(!flag){
        	// 生成响应报文
            jsonResult.put("responseCode", "01");
        	jsonResult.put("responseMsg", responseMsg);
        }
        logger.info("storeapi/designatedDriver responseJson:" + jsonResult);
        // 离开接口时间
        long endTime = System.currentTimeMillis();
        logger.info("storeapi/designatedDriver 离开接口:time=" + DateUtil.getStrDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS") + ";处理时长:" + (endTime - startTime));

        return jsonResult;
    }
	
	
	 /**
     * 服务范围校验
     */
	@ResponseBody
    @RequestMapping("/checkScope")
    public JSONObject checkScope(HttpServletRequest request) throws Exception {
		JSONObject jsonResult = new JSONObject();
        // 进入接口时间
        long startTime = System.currentTimeMillis();
        logger.info("storeapi/checkScope 进入接口:time=" + DateUtil.getStrDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));

        Boolean flag = true;
        String responseMsg = "成功";
        //读取报文
        try {
            // 设置编码
            request.setCharacterEncoding("UTF-8");
            // 读取请求报文
            String requestJson = IOUtils.toString(request.getInputStream(), "UTF-8");
            requestJson = URLDecoder.decode(requestJson, "UTF-8");
            if (requestJson != null && !"".equals(requestJson)) {
                // 调用业务处理 =============================
            	logger.info("storeapi/checkScope requestJson:" + requestJson);
                JSONObject requestBody = JSONObject.parseObject(requestJson);
                Map<String, Object> requestBodyMap = requestBody.toJavaObject(Map.class);
                //请求下游
                flag = true;
                jsonResult = checkScopeService.checkScope(requestBodyMap); 
                logger.info("storeapi/checkScope： "+jsonResult);
            } else {
                flag = false;
                responseMsg = "接口处理异常:请求报文为空";
                logger.error("storeapi/checkScope 接口处理异常:请求报文为空");
            }
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
            responseMsg = "数据接收异常";
            logger.error("storeapi/checkScope 数据接收异常:" + e.getMessage());
        }
        if(!flag){
        	// 生成响应报文
            jsonResult.put("responseCode", "01");
        	jsonResult.put("responseMsg", responseMsg);
        }
        logger.info("storeapi/checkScope responseJson:" + jsonResult);
        // 离开接口时间
        long endTime = System.currentTimeMillis();
        logger.info("storeapi/checkScope 离开接口:time=" + DateUtil.getStrDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS") + ";处理时长:" + (endTime - startTime));

        return jsonResult;
    }
	
	
	/**
     * 服务取消
     */
	@ResponseBody
    @RequestMapping("/driverCancel")
    public JSONObject driverCancel(HttpServletRequest request) throws Exception {
		JSONObject jsonResult = new JSONObject();
        // 进入接口时间
        long startTime = System.currentTimeMillis();
        logger.info("storeapi/driverCancel 进入接口:time=" + DateUtil.getStrDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));

        Boolean flag = true;
        String responseMsg = "成功";
        //读取报文
        try {
            // 设置编码
            request.setCharacterEncoding("UTF-8");
            // 读取请求报文
            String requestJson = IOUtils.toString(request.getInputStream(), "UTF-8");
            requestJson = URLDecoder.decode(requestJson, "UTF-8");
            if (requestJson != null && !"".equals(requestJson)) {
                // 调用业务处理 =============================
            	logger.info("storeapi/driverCancel requestJson:" + requestJson);
                JSONObject requestBody = JSONObject.parseObject(requestJson);
                Map<String, String> requestBodyMap  = requestBody.toJavaObject(Map.class);
                //请求下游
                flag = true;
                Map<String, String> map = driverCancelService.driverCancel_V2(requestBodyMap); 
                String jsonStr = JSON.toJSONString(map);
                jsonResult = JSONObject.parseObject(requestJson);
                logger.info("storeapi/driverCancel： "+map);
            } else {
                flag = false;
                responseMsg = "接口处理异常:请求报文为空";
                logger.error("storeapi/driverCancel 接口处理异常:请求报文为空");
            }
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
            responseMsg = "数据接收异常";
            logger.error("storeapi/driverCancel 数据接收异常:" + e.getMessage());
        }
        if(!flag){
        	// 生成响应报文
            jsonResult.put("responseCode", "01");
        	jsonResult.put("responseMsg", responseMsg);
        }
        logger.info("storeapi/driverCancel responseJson:" + jsonResult);
        // 离开接口时间
        long endTime = System.currentTimeMillis();
        logger.info("storeapi/driverCancel 离开接口:time=" + DateUtil.getStrDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS") + ";处理时长:" + (endTime - startTime));

        return jsonResult;
    }
}
